// Configuration for your app
// https://quasar.dev/quasar-cli/quasar-conf-js

module.exports = function (ctx) {
  // 从 .environments.json 中读取环境变量
  const envVars = loadEnvVars()

  return {
    // app boot file (/src/boot)
    // --> boot files are part of "main.js"
    boot: [
      'axios'
    ],

    css: [
      'app.styl'
    ],

    preFetch: false,

    extras: [
      // 'ionicons-v4',
      // 'mdi-v3',
      // 'fontawesome-v5',
      // 'eva-icons',
      // 'themify',
      // 'roboto-font-latin-ext', // this or either 'roboto-font', NEVER both!

      'roboto-font', // optional, you are not bound to it
      'material-icons' // optional, you are not bound to it
    ],

    framework: {
      // iconSet: 'ionicons-v4',
      // lang: 'de', // Quasar language

      // all: true, // --- includes everything; for dev only!

      components: [
        'QLayout',
        'QHeader',
        'QDrawer',
        'QPageContainer',
        'QPage',
        'QToolbar',
        'QToolbarTitle',
        'QBtn',
        'QBtnDropdown',
        'QIcon',
        'QList',
        'QItem',
        'QItemSection',
        'QItemLabel',
        'QBar',
        'QSpace',
        'QDate',
        'QForm',
        'QInput',
        'QField',
        'QPopupProxy',
        'QTabs',
        'QTab',
        'QRouteTab',
        'QRouteTab',
        'QTabPanels',
        'QTabPanel',
        'QBadge',
        'QMenu',
        'QSpinnerGears',
        'QExpansionItem',
        'QCard',
        'QCardSection',
        'QMarkupTable',
        'QTable',
        'QTh',
        'QTr',
        'QTd',
        'QScrollArea',
        'QPagination',
        'QInnerLoading',
        'QBreadcrumbs',
        'QBreadcrumbsEl',
        'QSlideTransition',
        'QTooltip',
        'QSelect',
        'QCheckbox',
        'QDialog',
        'QCard',
        'QCardSection',
        'QCardActions',
        'QColor',
        'QSeparator',
        'QTree',
        'QToggle',
        'QOptionGroup',
        'QImg',
        'QRadio',
        'QChip',
        'QTime',
        'QMarkupTable',
        'QSplitter',
        'QInfiniteScroll',
        'QSpinnerDots',
        'QCarousel',
        'QCarouselControl',
        'QCarouselSlide',
        'QAvatar',
        'QSlider',
        'QRange',
        'QBanner',
        'QChatMessage',
        'QVirtualScroll',
        'QPopupEdit',
        'QTimeline',
        'QTimelineEntry'
      ],

      directives: [
        'Ripple',
        'GoBack',
        'ClosePopup',
        'Mutation'
      ],

      // Quasar plugins
      plugins: [
        'Notify',
        'Loading',
        'LoadingBar',
        'Dialog'
      ],
      config: {
        loading: {
          spinner: 'QSpinnerGears'
        },
        loadingBar: {
          color: 'white'
        }
      }
    },

    supportIE: true,

    build: {
      scopeHoisting: true,
      vueRouterMode: 'history',
      // vueCompiler: true,
      // gzip: true,
      // analyze: true,
      // extractCSS: false,
      extendWebpack (cfg) {
        cfg.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /node_modules/,
          options: {
            formatter: require('eslint').CLIEngine.getFormatter('stylish')
          }
        }, {
          test: /node_modules[/\\](iconv-lite)[/\\].+/,
          resolve: {
            aliasFields: ['main']
          }
        })
      },
      chainWebpack: config => {
        config.plugins.delete('prefetch')
        // 在 chainWebpack 添加下面的一段代码
        config.externals({ './cptable': 'var cptable' })
      },
      // 若环境变量中指定了 COMPILE_ENV，则使用 COMPILE_ENV 为 env 赋值
      // 若未指定 COMPILE_ENV，则使用下方代码为 env 赋值
      env: envVars
    },

    devServer: {
      // https: true,
      // port: 8080,
      public: '', // proxies url
      open: true // opens browser window automatically
    },

    animations: 'all', // --- includes all animations
    // animations: [],

    ssr: {
      pwa: false
    },

    pwa: {
      // workboxPluginMode: 'InjectManifest',
      // workboxOptions: {}, // only for NON InjectManifest
      manifest: {
        // name: 'Quasar App',
        // short_name: 'Quasar App',
        // description: '使用quasar-cli搭建PC端开发环境',
        display: 'standalone',
        orientation: 'portrait',
        background_color: '#ffffff',
        theme_color: '#027be3',
        icons: [
          {
            'src': 'statics/icons/icon-128x128.png',
            'sizes': '128x128',
            'type': 'image/png'
          },
          {
            'src': 'statics/icons/icon-192x192.png',
            'sizes': '192x192',
            'type': 'image/png'
          },
          {
            'src': 'statics/icons/icon-256x256.png',
            'sizes': '256x256',
            'type': 'image/png'
          },
          {
            'src': 'statics/icons/icon-384x384.png',
            'sizes': '384x384',
            'type': 'image/png'
          },
          {
            'src': 'statics/icons/icon-512x512.png',
            'sizes': '512x512',
            'type': 'image/png'
          }
        ]
      }
    },

    cordova: {
      // id: 'org.cordova.quasar.app',
      // noIosLegacyBuildFlag: true, // uncomment only if you know what you are doing
    },

    electron: {
      bundler: 'builder', // or 'packager'

      extendWebpack (cfg) {
        // do something with Electron main process Webpack cfg
        // chainWebpack also available besides this extendWebpack
      },

      packager: {
        // https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#options

        // OS X / Mac App Store
        // appBundleId: '',
        // appCategoryType: '',
        // osxSign: '',
        // protocol: 'myapp://path',

        // Windows: 'only'
        // win32metadata: { ... }
      },

      builder: {
        // https://www.electron.build/configuration/configuration
        productName: '语祯云工',
        appId: 'quasar-vue',
        'icon': 'src-electron/icons/icon.ico',
        asar: true,
        compression: 'maximum',
        win: {
          target: 'nsis'
        },
        nsis: {
          allowToChangeInstallationDirectory: true,
          menuCategory: true,
          allowElevation: false,
          oneClick: false,
          perMachine: true,
          // 安装图标
          'installerIcon': 'src-electron/icons/icon.ico',
          // 卸载图标
          'uninstallerIcon': 'src-electron/icons/icon.ico',
          // 安装时头部图标
          'installerHeaderIcon': 'src-electron/icons/icon.ico',
          // 创建桌面图标
          'createDesktopShortcut': true
        },
        // 应用更新配置
        publish: [{
          provider: 'generic',
          url: envVars.UPDATE_SERVICE_URL
        }]
      }
    }
  }
}

/**
 * 加载环境变量
 * <p>
 * 将加载以下内容：
 * <ul>
 * <li>COMPILE_ENV 对应的 .environments.json 中的变量</li>
 * <li>package.json 中的 version</li>
 * </ul>
 *
 * @returns {*}
 */
function loadEnvVars () {
  let vars = require('./.environments.json')[process.env.COMPILE_ENV]
  vars.APP_VERSION = require('./package.json').version
  vars.COMPILE_ENV = process.env.COMPILE_ENV

  for (let key in vars) {
    vars[key] = JSON.stringify(vars[key])
  }

  return vars
}
